#include "hw_types.h"
#include "timer.h"
#include "osram96x16x1.h"
#include "hw_memmap.h"

#include "equation.h"

#include <string.h>

extern unsigned int seconds;
extern unsigned char state;
extern char equation[30];
extern char coeffA[10] ;
extern char coeffB[10] ;
extern char coeffC[10] ;

//
//Function creating a string from the three coefficients
//
void buildEquation(char coeffA[], char coeffB[], char coeffC[])
{
    int position = 0;
   
   	if (strlen(coeffA) > 0)
    {
        strncpy(equation + position, coeffA, strlen(coeffA));
        position = strlen(coeffA);
        equation[position++] = 'x';
       
    }

    if (strlen(coeffB) > 0)
    {
       
        if (strlen(coeffA) && coeffB[0] != '-')
           equation[position++] = '+';
       
       
        strncpy(equation + position, coeffB, strlen(coeffB));
   
        position += strlen(coeffB);
        equation[position++] = 'x';
    }

    if (strlen(coeffC) > 0)
    {
       
        if ((strlen(coeffA) || strlen(coeffB)) && coeffC[0] != '-')
            equation[position++] = '+';
       
       
       strncpy(equation + position, coeffC, strlen(coeffC));
       position += strlen(coeffC);       
    }
    
	equation[position] = '\0';
}

void moveEquation(char polynom[])
{
	unsigned short lenght;
	short posX, j, ovr_left, index = -2, pos_square, flag2, count;
	char *ptrCh1;
	int result, oldResult;
	long i =0;
	
	if (strlen(coeffA))					  //
	{									  //
		for (i=0; i<10; i++)			  //Determines position of 'x' symbol
		{								  //for A coefficient.
			if (polynom[i] == 'x')		  //index stores the position.
			{							  //This is used for diplaying 2nd power of the polynom
				index = i;				  //If A coefficient is entered index is with value -2
				break;					  //
			}							  //
		}								  //
	}
	oldResult = GetDataFromACP(); 
	lenght = strlen(polynom) + 16;// +16 symbols for blank space after the polynom end
	strcat(polynom," ");	// after the polynom should be a whitespace
	while(1)
	{	
		result = GetDataFromACP();
			    
			ptrCh1 = polynom;			 //pointer to string that contents the polynom
			ovr_left = 1;				 //used to show the rest of polynom when the begining reaches left LED side
			j = 0;						 //control variable
			posX = 96;					 //shows the current pixel on LED
		    pos_square = 0;				 //used for position of 2nd power symbol in LED
			flag2 = 0;					 //used to show if we have  coefficient A 
			count = 0;					 //used to determine when the 'x' symbol for A coefficient to appear
			
			while(j != lenght)
			{
				if(oldResult!=result || state)	  // Checks for an activity from a button or a potentiometer
				{
					TimerEnable(TIMER2_BASE, TIMER_A);
					seconds=0;
					state=0;
					OSRAM96x16x1Clear();
					return;
				}
				result = GetDataFromACP();
			
				if(posX != 0)
				{
					ptrCh1 = polynom;
					posX -= 6;
					pos_square = posX + (index*6) + 3;			//Calculates where on LED to display 2nd power
					if(count == (index + 1) && pos_square >= 0)	  //calculates when to display 2nd power if index != 0(entered A) 
					{
						OSRAM96x16x1StringDraw("2 ",pos_square ,0);
						flag2 = 1;
					}
					if(!flag2)
						++count;					
					OSRAM96x16x1StringDraw(ptrCh1,posX ,1);//Displaying the polynom 
					++j;				  	  //j initialization after 6 pixels

					if(  (oldResult!=result || state))// Checks for an activity from a button or a potentiometer
					{
						TimerEnable(TIMER2_BASE, TIMER_A);
						seconds=0;
						state=0;
						OSRAM96x16x1Clear();
						return;
					}
					result = GetDataFromACP();			
					for(i = 0; i < 120000;++i);//	 
											   // Delays for slower movement	 
			
				}
				else
				{	
					if(strlen(coeffA)  && pos_square >= 0) //Checks if A coefficient is entered
					{	
						if(pos_square) 					   //Checks if 2nd power is still needed to be displayed
						{
							pos_square -= 6;
							if (pos_square >= 0)
								OSRAM96x16x1StringDraw("2 ",pos_square ,0);
						}			
					}
					if (pos_square < 0)
						OSRAM96x16x1StringDraw("  ",0 ,0);	//shows blank space when 2nd power should not be shown
					ptrCh1 = polynom + ovr_left;			//determine what part of the string to be shown
					OSRAM96x16x1StringDraw(ptrCh1,posX,1);				
					++j;	
					++ovr_left;	
					for(i = 0; i < 120000;++i);//
											   //Delaying for slower movement
											   //
				} 
			}
		  OSRAM96x16x1Clear();	   //clears the display
	}
}
